package com.library.entities;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.JoinColumn;

@Entity
@Table(name = "BOOK")
@NamedQuery(name = "Book.findAll",
	query = "SELECT b FROM Book b ORDER BY b.name")
public class Book {
	
	@Id
	@GeneratedValue
	@Column(name = "BOOK_ID")
	private Integer id;
	
	@Column(name = "NAME", nullable = false)
	private String name;
	
	@Column(name = "ISBN")
	private String ISBN;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="BOOK_GENRE",
		joinColumns = @JoinColumn(name="BOOK_ID", referencedColumnName="BOOK_ID"),
		inverseJoinColumns = @JoinColumn(name="GENRE_ID", referencedColumnName="GENRE_ID")
	)
	private List<Genre> genreList; 
	
	@OneToOne(mappedBy = "book")
	private BookUsage bookUsage;
	
	public void setId(Integer id){
		this.id = id;
	}
	
	public Integer getId(){
		return id;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return name;
	}
	
	public void setISBN(String ISBN){
		this.ISBN = ISBN;
	}
	
	public String getISBN(){
		return ISBN;
	}
	
	public void setGenreList(List<Genre> genreList){
		this.genreList = genreList;
	}
	
	public List<Genre> getGenreList(){
		return genreList;
	}
	
	public static Builder getBuilder() {
		return new Builder();
	}

	public static class Builder {
		
		Book built;
		
		Builder(){
			built = new Book();
		}
		
		public Builder setName(String name){
			built.setName(name);
			return this;
		}
		
		public Builder setISBN(String ISBN){
			built.setISBN(ISBN);
			return this;
		}
		
		public Book build(){
			return built;
		}
	}
}
